package com.linko.intelligent.remote.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linko.intelligent.common.util.HttpUtil;
import com.linko.intelligent.remote.core.mapper.CategoryMapper;
import com.linko.intelligent.remote.core.mapper.DeviceInfoMapper;
import com.linko.intelligent.remote.core.mapper.IotLatLngMapper;
import com.linko.intelligent.remote.core.mapper.IotStatusMapper;
import com.linko.intelligent.remote.core.service.DeviceService;
import com.linko.intelligent.remote.pojo.po.Category;
import com.linko.intelligent.remote.pojo.po.DeviceInfo;
import com.linko.intelligent.remote.pojo.po.IotLatLng;
import com.linko.intelligent.remote.pojo.po.IotStatus;
import com.linko.intelligent.remote.pojo.query.DeviceInfoQuery;
import com.linko.intelligent.remote.pojo.vo.CategoryVo;
import com.linko.intelligent.remote.pojo.vo.DeviceInfoWithStatus;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class DeviceServiceImpl implements DeviceService {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(HttpUtil.class);
    @Autowired
    private DeviceInfoMapper deviceInfoMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private IotLatLngMapper iotLatLngMapper;

    @Autowired
    private IotStatusMapper iotStatusMapper;

    public List<CategoryVo> deviceTree() {
        QueryWrapper query = new QueryWrapper();
        List<Category> categories = categoryMapper.selectList(query);

        QueryWrapper deviceQuery = new QueryWrapper();
        deviceQuery.eq("device_type", 1);//只查询车
        List<DeviceInfo> deviceInfos = deviceInfoMapper.selectList(deviceQuery);

        List<CategoryVo> categorieVos = new ArrayList<>();
        for (Category category : categories) {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category, categoryVo);
            categorieVos.add(categoryVo);
        }
        return assembleTree(categorieVos, deviceInfos);
    }

    @Override
    public List<DeviceInfo> deviceList(DeviceInfoQuery query) {
        List<DeviceInfo> deviceInfos = deviceInfoMapper.selectLis(query);
        return deviceInfos;
    }

    @Override
    public PageInfo<DeviceInfo> page(DeviceInfoQuery query) {
        PageInfo<DeviceInfo> pageInfo = PageHelper.startPage(query.getPn(), query.getPs()).doSelectPageInfo(
                () -> deviceInfoMapper.selectLis(query)
        );
        return pageInfo;
    }

    @Override
    public DeviceInfoWithStatus deviceDetail(String sn) {
        List<DeviceInfoWithStatus> deviceList = deviceInfoMapper.selectBySn(sn);
        if (deviceList.size() > 0) {
            DeviceInfoWithStatus device = deviceList.get(0);
            List<IotLatLng> iotLatLng = iotLatLngMapper.selectLast();
            List<IotStatus> iotStatus = iotStatusMapper.selectLast();
            if (iotLatLng.size() > 0) {
                device.setIotLatLng(iotLatLng.get(0));
            }
            if (iotStatus.size() > 0) {
                device.setIotStatus(iotStatus.get(0));
            }
            return device;
        } else {
            return null;
        }

    }

    public List<CategoryVo> assembleTree(List<CategoryVo> categoryList, List<DeviceInfo> deviceInfoList) {
        // 创建一个按id存储类别的映射
        Map<Long, CategoryVo> categoryMap = new HashMap<>();
        for (CategoryVo category : categoryList) {
            categoryMap.put(category.getOid(), category);
        }

        // 创建一个按cateId存储设备信息的映射
        Map<Long, List<DeviceInfo>> deviceInfoMap = new HashMap<>();
        for (DeviceInfo deviceInfo : deviceInfoList) {
            Long cateId = deviceInfo.getCateId();
            if (!deviceInfoMap.containsKey(cateId)) {
                deviceInfoMap.put(cateId, new ArrayList<>());
            }
            deviceInfoMap.get(cateId).add(deviceInfo);
        }

        // 组装树
        List<CategoryVo> rootCategories = new ArrayList<>();
        for (CategoryVo category : categoryList) {
            Long parentId = category.getPid();
            if (parentId == null || parentId == 0) {
                rootCategories.add(category);
            } else {
                CategoryVo parentCategory = categoryMap.get(parentId);
                if (parentCategory != null) {
                    parentCategory.addChild(category);
                }
            }

            // 将设备信息附加到相应的类别
            List<DeviceInfo> devices = deviceInfoMap.get(category.getId());
            if (devices != null) {
                category.setChildren(new ArrayList<>(devices)); // 使用新的ArrayList来保存设备信息
            }
        }

        return rootCategories;
    }
}


